package com.sh.controller.admin;

import com.sh.common.BaseResult;
import com.sh.pojo.bo.UpdatePermissionBO;
import com.sh.service.permission.IUserPermissionService;
import com.sh.service.store.IOSService;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/permission")
public class AdminPermissionController {
    @Autowired
    private IUserPermissionService userPermissionService;
    @Autowired
    private IOSService osService;

    /**
     * 查询所有桶
     * */
    @GetMapping("/bucket/list")
    public BaseResult listBucket() {
        return BaseResult.success(osService.listBuckets());
    }


    /**
     * 查询用户桶下文件夹权限
     * */
    @GetMapping("/get")
    public BaseResult get(@RequestParam(name = "userId")String userId,@RequestParam(name = "bucket") String bucket,@RequestParam (name = "path",required = false) @Pattern(regexp = ".*/$", message = "路径需要以'/'结束")String path) {

        return BaseResult.success(userPermissionService.getUserPermissionByBucket(userId, bucket,path));
    }

    @PostMapping("/update")
    public BaseResult update(@Validated @RequestBody UpdatePermissionBO bo){
        userPermissionService.setUserPermission(bo);
        return BaseResult.success();
    }
}
